//
//  SportPlanNet.m
//  SportSocial
//
//  Created by yull on 15/2/4.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "SportPlanNet.h"
#import <CoreLocation/CoreLocation.h>

@implementation SportPlanNet

+ (AFHTTPRequestOperation *)uploadHistoryWithSportName:(NSString *)sportName
                                             startDate:(NSString *)startDate
                                               endDate:(NSString *)endDate
                                             longitude:(CGFloat)longitude
                                              latitude:(CGFloat)latitude
                                               calorie:(NSUInteger)calorie
                                          successBlock:(httpSuccessBlock)successBlock
                                             failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/uploadHistory",[self baseUrl]];
    NSDictionary *param = @{@"sport_name":sportName,
                            @"start_date":startDate,
                            @"end_date":endDate,
                            @"longitude":[NSNumber numberWithFloat:longitude],
                            @"latitude":[NSNumber numberWithFloat:latitude],
                            @"calorie":@(calorie)};
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:@[param] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *dataJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    param = @{@"token":[CurrentUser shareInstance].token,
              @"data":dataJson};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadHistoryWithSportName:(NSString *)sportName
                                             startDate:(NSString *)startDate
                                               endDate:(NSString *)endDate
                                             longitude:(CGFloat)longitude
                                              latitude:(CGFloat)latitude
                                               calorie:(NSUInteger)calorie
                                              distance:(CGFloat)distance
                                              maxSpeed:(CGFloat)maxSpeed
                                              avgSpeed:(CGFloat)avgSpeed
                                             locations:(NSArray *)locations
                                          successBlock:(httpSuccessBlock)successBlock
                                             failBlock:(httpFailBlock)failBlock
{
    
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/uploadHistory",[self baseUrl]];
   
    NSDictionary *param = @{@"sport_name":sportName,
                            @"start_date":startDate,
                            @"end_date":endDate,
                            @"longitude":[NSNumber numberWithFloat:longitude],
                            @"latitude":[NSNumber numberWithFloat:latitude],
                            @"calorie":@(calorie),
                            @"distance":[NSNumber numberWithFloat:distance],
                            @"max_speed":[NSNumber numberWithFloat:maxSpeed],
                            @"avg_speed":[NSNumber numberWithFloat:avgSpeed],
                            @"locations":locations};
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:@[param] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *dataJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    param = @{@"token":[CurrentUser shareInstance].token,
              @"data":dataJson};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getHistoryWithUserId:(UInt64)userId
                                      pageNumber:(NSUInteger)pageNumber
                                        pageSize:(NSUInteger)pageSize
                                    successBlock:(httpSuccessBlock)successBlock
                                       failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/sport/paginateHistory", [self baseUrl]];
    
    NSDictionary *params = @{@"token":[self token],
                             @"page_number":@(pageNumber),
                             @"page_size":@(pageSize),
                             @"user_id":@(userId)};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getHobbyListWithUserId:(UInt64)userId
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/sport/listHobby", [self baseUrl]];
    
    NSDictionary *params = @{@"token":[self token],
                             @"user_id":@(userId)};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

@end
